package com.tarena.security.admin.service.impl;


import com.tarena.security.admin.converter.MicroServiceConverter;
import com.tarena.security.admin.entity.db.App;
import com.tarena.security.admin.entity.db.Department;
import com.tarena.security.admin.entity.db.MicroService;
import com.tarena.security.admin.entity.param.MicroServiceParam;
import com.tarena.security.admin.entity.query.MicroServiceQuery;
import com.tarena.security.admin.errorenum.ResultEnum;
import com.tarena.security.admin.exception.SecurityAdminBusinessException;
import com.tarena.security.admin.persists.repository.MicroServiceRepository;
import com.tarena.security.admin.service.IMicroServiceService;

import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * <p>
 * micro_service 服务实现类
 * </p>
 *
 * @author Ly
 * @since 2023-04-10
 */
@Service
@Slf4j
public class MicroServiceServiceImpl implements IMicroServiceService {

    @Resource
    private MicroServiceRepository  microServiceRepository;

    @Resource
    private MicroServiceConverter microServiceConverter;


    @Override public void deleteMicroServiceById(Integer[] id) {
        microServiceRepository.deleteById(id);
    }

    @Override public MicroService getMicroServiceById(Integer id) {
        return microServiceRepository.getMicroServiceById(id);
    }

    @Override public void updateByID(MicroServiceParam param) {
        MicroService microService=microServiceRepository.getMicroServiceById(param.getId());

        if (microService==null){
            throw new SecurityAdminBusinessException(ResultEnum.SYS_MICROSERVICE_NON_EXISTENT);
        }
        int count = microServiceRepository.getMicroServiceCountByName(param.getName());
        if (count!=0){
            throw new SecurityAdminBusinessException(ResultEnum.SYS_MICROSERVICE_ALREADY_EXISTS);
        }
        microService= microServiceConverter.convertParamToModel(param);
        microService.setUpdateTime(System.currentTimeMillis());
        log.info("Department:{}",microService);

        int row=microServiceRepository.updateByID(microService);
        if (row!=1){
            throw new SecurityAdminBusinessException(ResultEnum.SYSTEM_ERROR);
        }
    }

    @Override public void addNewMicroService(MicroServiceParam param) {
        int count=microServiceRepository.getMicroServiceCountByName(param.getName());
        log.info("查询结果为：{}",count);
        if (count>0){
            throw new SecurityAdminBusinessException(ResultEnum.SYS_MICROSERVICE_ALREADY_EXISTS);
        }
        MicroService microService= microServiceConverter.convertParamToModel(param);
        microService.setCreateUserId(1);
        microService.setCreateTime(System.currentTimeMillis());
        microService.setUpdateTime(System.currentTimeMillis());
        count=microServiceRepository.addNewMicroService(microService);
        if (count!=1){
            throw new SecurityAdminBusinessException(ResultEnum.SYSTEM_ERROR);
        }
    }

    @Override public List<MicroService> getMicroServiceList(MicroServiceQuery query) {
        List<MicroService> list =microServiceRepository.getMicroServiceList(query);
        return list;
    }
}
